import { app, BrowserWindow, ipcMain } from 'electron'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */

import { updateHandle } from '../renderer/utils/update.js'

if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path')
    .join(__dirname, '/static')
    .replace(/\\/g, '\\\\')
}

let mainWindow
const winURL =
  process.env.NODE_ENV === 'development'
    ? `http://localhost:9527`
    : `file://${__dirname}/index.html`

function createWindow() {
  /**
   * Initial window options
   */
  //   mainWindow = new BrowserWindow({
  //     height: 583,
  //     width: 800,
  //     useContentSize: true,
  //     movable: true
  //   })
  mainWindow = new BrowserWindow({
    width: 600,
    height: 500,
    useContentSize: true,
    transparent: false,
    frame: false, // PS:隐藏窗口菜单
    resizable: false,
    titleBarStyle: 'hidden',
    movable: true,
    webPreferences: { webSecurity: false }
  })
  mainWindow.loadURL(winURL)
  //   监听是否是登录窗口
  ipcMain.on('login', () => {
    // 改变窗口大小
    // 重新设置窗口的位置
    mainWindow.setSize(600, 500)
    mainWindow.center()
  })
  ipcMain.on('loginout', () => {
    // 改变窗口大小
    // mainWindow.setSize(1920, 1080)
    mainWindow.unmaximize()
  })
  ipcMain.on('dashboard', () => {
    // 改变窗口大小
    // mainWindow.setSize(1920, 1080)
    mainWindow.maximize()
  })
  // 最小化窗体
  ipcMain.on('minWindow', () => {
    console.log('main 最小化')
    mainWindow.minimize()
  })
  // 关闭窗体
  ipcMain.on('closeWindow', () => {
    mainWindow.close()
    // if (process.platform !== 'darwin') {
    //   app.quit()
    // }
  })
  // 最大化窗体
  ipcMain.on('maxWindow', () => {
    mainWindow.isMaximized() ? mainWindow.unmaximize() : mainWindow.maximize()
  })
  // 在主线程下，通过ipcMain对象监听渲染线程传过来的getPrinterList事件
  ipcMain.on('getPrinterList', (event) => {
    // 主线程获取打印机列表
    const list = window.webContents.getPrinters()
    console.log(list)
    // 通过webContents发送事件到渲染线程，同时将打印机列表也传过去
    // window.webContents.send('getPrinterList', list)
  })

  // Open dev tools initially when in development mode
  if (process.env.NODE_ENV === 'development') {
    mainWindow.webContents.on('did-frame-finish-load', () => {
      mainWindow.webContents.once('devtools-opened', () => {
        mainWindow.focus()
      })
      mainWindow.webContents.openDevTools()
    })
  }
  // 设置版本更新地址，即将打包后的latest.yml文件和exe文件同时放在
  // http://download.lnrfnf.com/update/对应的服务器目录下,该地址和package.json的publish中的url保持一致
  const feedUrl = 'http://download.lnrfnf.com/update/'
  // 检测版本更新
  updateHandle(mainWindow, feedUrl)
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
